﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using SPBusiness.ExtensionMethods;
using Microsoft.SharePoint.Taxonomy;

namespace SPBusiness.ManagedMetadata
{   
   public sealed class Taxonomy
    {
       /// <summary>
       /// Taxonomy class has only Static Methods, no constructor needed.
       /// MSOCAF: This assembly is called by Sandbox Proxy, which is partially trusted. APTCA is inevitable.
       /// Permission set Attribute is Demanding FullTrust for this namespace.
       /// </summary>
       private Taxonomy() { }
       //public static void SetTaxonomyField(string pSiteUrl, string pWebUrl, string vStaticFieldName, string vGroupName, string vTermSetName)
       // {
       //     try
       //     {               
       //         SPSecurity.RunWithElevatedPrivileges(delegate()
       //         {
       //             using (SPSite vSPSite = new SPSite(pSiteUrl))
       //             {
       //                 using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
       //                 {

       //                     SPField vSPField = vSPWeb.Fields.TryGetFieldByStaticName(vStaticFieldName);

       //                     if (vSPField != null)
       //                     {
       //                         TaxonomySession session = new TaxonomySession(vSPSite);

       //                         if (session.TermStores.Count != 0)
       //                         {
       //                             var termStore = session.TermStores[session.TermStores[0].Id];

       //                             Microsoft.SharePoint.Taxonomy.Group vGroup = null;
       //                             if (!termStore.Groups.Any(x => x.Name == vGroupName))
       //                             {
       //                                 vGroup = termStore.CreateGroup(vGroupName);
       //                             }
       //                             else 
       //                             {
       //                                 vGroup = termStore.Groups[vGroupName];
       //                             }

       //                             Microsoft.SharePoint.Taxonomy.TermSet vTermSet = null;
       //                             if (!vGroup.TermSets.Any(x => x.Name == vTermSetName))
       //                             {
       //                                 vTermSet = vGroup.CreateTermSet(vTermSetName);
       //                             }
       //                             else
       //                             {
       //                                 vTermSet = vGroup.TermSets[vTermSetName];
       //                             }

       //                             termStore.CommitAll();

       //                             TaxonomyField vTaxonomyField = vSPWeb.Fields[vSPField.Id] as TaxonomyField;

       //                             vTaxonomyField.SspId = vTermSet.TermStore.Id;
       //                             vTaxonomyField.TermSetId = vTermSet.Id;
       //                             vTaxonomyField.TargetTemplate = string.Empty;
       //                             vTaxonomyField.AnchorId = Guid.Empty;
       //                             vTaxonomyField.AllowMultipleValues = true;
       //                             vTaxonomyField.CreateValuesInEditForm = true;
       //                             vTaxonomyField.Open = true;
       //                             vTaxonomyField.Update(true);
       //                         }
       //                     }
       //                 }
       //             }
       //         });
       //     }
       //     catch (Exception ex)
       //     {
       //        Logging.LogMessase("SPBusiness.ManagedMetadata", "Taxonomy", "SetTaxonomyField", ex);
       //         throw;
       //     }
       // }

    }
}
